package com.ruoyi.web.controller.dish;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Snowflake;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.dish.domain.DishMenu;
import com.ruoyi.dish.domain.vo.MenuSearchVo;
import com.ruoyi.dish.domain.vo.MenuVo;
import com.ruoyi.dish.service.DishMenuService;
import com.ruoyi.human.domain.Count;
import com.ruoyi.human.domain.HuStore;
import com.ruoyi.human.service.HuStoreService;
import com.ruoyi.human.service.impl.CounterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

import static com.ruoyi.common.utils.PageUtils.startPage;


@RestController
public class MenuController extends BaseController {
    @Autowired
    private DishMenuService dishMenuService;
    @Autowired
    private Snowflake snowflake;
    @Autowired
    private HuStoreService storeService;
    @Autowired
    private CounterService counterService;

    @PostMapping("/dish/menu/add")
    public String addMenu(@RequestBody MenuVo menuVo) {
        DishMenu dishMenu=new DishMenu();
        //在human数据库中的tb_count数据表中将对应法商家id后面的count加一
        int i=counterService.updateCount(menuVo.getStoreId());
        if(i==0){
            return "添加失败";
        }

        //根据商店id查询商店信息
        HuStore store = storeService.getStoreById(menuVo.getStoreId());
        //菜单名称用雪花算法
        dishMenu.setMenuId(snowflake.nextIdStr());
        //菜品名称和价格和商店id自己输入
        //将menuVo中的数据复制到dishMenu中
        BeanUtil.copyProperties(menuVo,dishMenu);
        //创建时间为当前时间
        dishMenu.setLastUpdateTime(DateUtils.getNowDate());
        //将dishMenu中的数据添加到数据库中
        dishMenuService.insert(dishMenu);
        return "success";
    }
    @PostMapping("/dish/menu/list")
    public TableDataInfo listMenu() {
        startPage(); // 设置分页信息
        List<DishMenu> menuList = dishMenuService.selectMenuList();
        return getDataTable(menuList);
    }
    //模糊搜索
    @PostMapping("/dish/menu/search")
    public TableDataInfo searchMenu(@RequestBody MenuSearchVo menuSearchVo) {
        startPage(); // 设置分页信息
        List<DishMenu> menuList = dishMenuService.searchMenu(menuSearchVo);
        return getDataTable(menuList);
    }


}
